import React from 'react';
import { connect, ReactReduxContext } from 'react-redux';
import { Router } from 'react-router';
import { locationChange } from './actions';

// 依赖store
class ConnectedRouter extends React.Component {
    componentDidMount() {
        // 当路径发生变化的时候，会执行回调，传递最新的location和action
        this.props.history.listen((location, action) => {
            // 派发事件
            this.props.dispatch(locationChange(location, action));
        })
    }
    render() {
        const { history, children } = this.props;
        return (
            <Router history={history}>
                { children }
            </Router>
        )
    }
}

export default connect(state => state)(ConnectedRouter);